package leetCode;

import java.util.*;

public class nextMax {
    public static void main(String[] args) {

//        int[] nums = new int[]{4,2,0,2,3,2,0};
//        Arrays.sort(nums,0,3);
////        System.out.println(nums);
//        for(int num :nums){
//            System.out.println(num);
//        }
//        nextPermutation(nums);
//        for(int num : nums){
//            System.out.print(num+"  ");
//        }
        String s = "()(()";
        System.out.println(longestValidParentheses(s));
    }
    public static int longestValidParentheses(String s) {
        int maxans = 0;
        Deque<Integer> stack = new LinkedList<Integer>();
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push(i);
            } else {
                stack.pop();
                if (stack.isEmpty()) {
                    stack.push(i);
                } else {
                    maxans = Math.max(maxans, i - stack.peek());
                }
            }
        }
        return maxans;
    }


    public static void nextPermutation(int[] nums) {
        int minIndex = -1;
        int min = 20;
        for(int i = nums.length-2;i>=0;i--){
            for(int j = i+1;j<nums.length;j++){
                if(nums[j] < min && nums[j] > nums[i]){
                    min = nums[j];
                    minIndex = j;
                }
            }
            if(minIndex != -1){
                int temp = nums[i];
                nums[i] = nums[minIndex];
                nums[minIndex] = temp;
                Arrays.sort(nums,i+1,nums.length);
                return;
            }
        }
        Arrays.sort(nums);
    }


}
